#include "widget_replay.h"



replay_widget::replay_widget( QWidget* parent, Qt::WindowFlags flags ): QWidget( parent, flags )
{
	layout = new QVBoxLayout( this );


	QWidget* title_w = new QWidget( this );
	QHBoxLayout* title_w_l = new QHBoxLayout( title_w );
	
	QLabel* title_l = new QLabel( title_w );
	title_l->setTextFormat( Qt::RichText );
	title_l->setText( "<b><font size=8>Replay game</font></b>" );
	title_w_l->addWidget( title_l );

	next_b = new QPushButton( "NEXT", title_w );
	next_b->setFixedWidth( 150 );
	next_b->setEnabled( false );
	title_w_l->addWidget( next_b );
	connect( next_b, SIGNAL( clicked() ), this, SLOT( next() ) );

	title_w->setLayout( title_w_l );
	layout->addWidget( title_w );


	layout->addSpacing( 20 );


	QLabel* players_l = new QLabel( this );
	players_l->setTextFormat( Qt::RichText );
	players_l->setText( "<b><font size=6>Players commands loading</font></b>" );
	layout->addWidget( players_l );


	QWidget* players_w = new QWidget( this );
	QHBoxLayout* players_w_l = new QHBoxLayout( players_w );

	players_i = new QLineEdit( players_w );
	players_i->setFixedWidth( 250 );
	players_i->setText( "replays/replay.rpl" );
	players_w_l->addWidget( players_i );

	players_filename_b = new QPushButton( "...", players_w );
	players_filename_b->setFixedWidth( 20 );
	players_w_l->addWidget( players_filename_b );
	connect( players_filename_b, SIGNAL( clicked() ), this, SLOT( load_filename_players() ) );

	players_b = new QPushButton( "Load", players_w );
	players_b->setFixedWidth( 150 );
	players_w_l->addWidget( players_b );
	connect( players_b, SIGNAL( clicked() ), this, SLOT( load_players() ) );

	players_w->setLayout( players_w_l );
	layout->addWidget( players_w );


	QLabel* map_l = new QLabel( this );
	map_l->setTextFormat( Qt::RichText );
	map_l->setText( "<b><font size=6>Map loading</font></b>" );
	layout->addWidget( map_l );


	QWidget* map_w = new QWidget( this );
	QHBoxLayout* map_w_l = new QHBoxLayout( map_w );

	map_i = new QLineEdit( map_w );
	map_i->setFixedWidth( 250 );
	map_i->setText( "maps/map.map" );
	map_w_l->addWidget( map_i );

	map_filename_b = new QPushButton( "...", map_w );
	map_filename_b->setFixedWidth( 20 );
	map_w_l->addWidget( map_filename_b );
	connect( map_filename_b, SIGNAL( clicked() ), this, SLOT( load_filename_map() ) );

	map_b = new QPushButton( "Load", map_w );
	map_b->setFixedWidth( 150 );
	map_w_l->addWidget( map_b );
	connect( map_b, SIGNAL( clicked() ), this, SLOT( load_map() ) );

	map_w->setLayout( map_w_l );
	layout->addWidget( map_w );


	QLabel* object_l = new QLabel( this );
	object_l->setTextFormat( Qt::RichText );
	object_l->setText( "<b><font size=6>Objects loading</font></b>" );
	layout->addWidget( object_l );


	QWidget* object_w = new QWidget( this );
	QHBoxLayout* object_w_l = new QHBoxLayout( object_w );

	object_i = new QLineEdit( object_w );
	object_i->setFixedWidth( 250 );
	object_i->setText( "objects/objects.obj" );
	object_w_l->addWidget( object_i );

	object_filename_b = new QPushButton( "...", object_w );
	object_filename_b->setFixedWidth( 20 );
	object_w_l->addWidget( object_filename_b );
	connect( object_filename_b, SIGNAL( clicked() ), this, SLOT( load_filename_objects() ) );

	object_b = new QPushButton( "Load", object_w );
	object_b->setFixedWidth( 150 );
	object_b->setEnabled( false );
	object_w_l->addWidget( object_b );
	connect( object_b, SIGNAL( clicked() ), this, SLOT( load_objects() ) );

	object_w->setLayout( object_w_l );
	layout->addWidget( object_w );


	this->setLayout( layout );
	//this->setMaximumSize( 550, 334 );
};



void replay_widget::load_filename_players()
{
	players_i->setText( QFileDialog::getOpenFileName( this, "Load commands", "./replays", "Commands files (*.rpl)" ) );
};



void replay_widget::load_filename_map()
{
	map_i->setText( QFileDialog::getOpenFileName( this, "Load map", "./maps", "Map files (*.map)" ) );
};



void replay_widget::load_filename_objects()
{
	object_i->setText( QFileDialog::getOpenFileName( this, "Load objects", "./objects", "Objects files (*.obj)" ) );
};



void replay_widget::load_players()
{
	if( players::get_instance()->load_players( players_i->text().toStdString().c_str() ) )
	{
		players_b->setEnabled( false );
		players_filename_b->setEnabled( false );
		players_i->setReadOnly( true );
		//map and players loaded
		object_b->setEnabled( !map_b->isEnabled() && !players_b->isEnabled() );
		players_b->setText( "OK!" );
	}
};



void replay_widget::load_map()
{
	if( mapa::get_instance()->load( map_i->text().toStdString().c_str() ) )
	{
		map_b->setEnabled( false );
		map_filename_b->setEnabled( false );
		map_i->setReadOnly( true );
		//map and players loaded
		object_b->setEnabled( !map_b->isEnabled() && !players_b->isEnabled() );
		map_b->setText( "OK!" );
	}
};



void replay_widget::load_objects()
{
	if( mapa::get_instance()->load_objects( object_i->text().toStdString().c_str() ) )
	{
		object_b->setEnabled( false );
		object_filename_b->setEnabled( false );
		object_i->setReadOnly( true );
		object_b->setText( "OK!" );
		next_b->setEnabled( true );
	}
};



void replay_widget::next()
{
	game_gui::get_instance()->start_replay();
};